{% extends 'base_backend.html' %}
{% load static %}

{% block header_tail %}
    <link rel="stylesheet" href="{% static 'public/CodeMirror/lib/codemirror.css' %}">
    <link rel="stylesheet" href="{% static 'public/CodeMirror/theme/lesser-dark.css' %}">
    <link rel="stylesheet" href="{% static 'public/html5shiv/css/jquery.slinky.css' %}">
    <style>
        .debug-radio {
            width: 16px;
            height: 16px;
            vertical-align: text-bottom;
        }

        #menu ul {
            padding: 0;
            height: 100%;
            /* background-color: #aaa */
        }

        .menu-num {
            float: right;
            cursor: pointer;
            margin-right: 20px;
        }

        .menu-title {
            margin-left: 20%;
        }

        .menu-back {
            border-bottom: solid 1px cadetblue;
        }

        .menu-back-a {
            color: #00acd6;
            cursor: pointer;
        }

        .text-error {
            color: red;
        }

        .menu-urls-num {
            float: right;
            margin-right: 30px;
        }
    </style>
{% endblock header_tail %}

{% block section_content %}
    <div class="row" style="height: 100%">
        <!-- code -->
        <section id="codeSection" class="col-md-6 connectedSortable">
            <div class="box box-success">
                <div class="box-header with-border">
                    <h3 class="box-title">{{ config_id }}爬虫代码</h3>
                    <button id="turn" class="btn btn-xs btn-warning"><i class="fa fa-retweet">转屏</i></button>

                </div>
                <div class="box-body">
                    <textarea id="codeView" class="form-control" rows="35"
                              placeholder="请输入代码">{{ code }}</textarea>
                </div>
                <div class="box-footer">
                    <div class="form-group">
                        <p style="display: none" id="config-flag">{{ config.config_flag }}</p>
                        <p style="display: none" id="entrance_url">{{ config.entrance_url }}</p>
                        <p style="display: none" id="api_debug_url">{{ api_debug_url }}</p>
                        <p style="display: none" id="api_debug_result_url">{{ api_debug_result_url }}</p>
                        <p style="display: none" id="api_debug_kill_url">{{ api_debug_kill_url }}</p>
                        <label>代理</label>
                        <input type="radio" name="proxy" value="0" class="debug-radio" checked>不使用
                        <input type="radio" name="proxy" value="1" class="debug-radio">使用
                        <span style="margin-left: 10px"></span>
                        <label>环境</label>
                        <input type="radio" name="env" value="0" class="env-radio" checked>国内
                        <input type="radio" name="env" value="1" class="env-radio">国外
                        <button id="kill" class="btn btn-md btn-default pull-right" style="margin-left: 10px">停止
                        </button>
                        <button id="breakpoint" class="btn btn-md btn-success pull-right" style="margin-left: 10px">分步
                        </button>
                        <button id="continue" class="btn btn-md btn-success pull-right" style="margin-left: 10px">长测
                        </button>
                        <button id="single" class="btn btn-md btn-success pull-right" style="margin-left: 10px">单测
                        </button>
                    </div>
                </div>
            </div>
        </section>
        <!-- result -->
        <section id="resultSection" class="col-md-6 connectedSortable">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title">测试结果</h3>
                    <span id="status" class="badge">loading...</span>
                </div>
                <div class="box-body" style="height: 785px">
                    <textarea id="result" class="slinky-menu form-control" rows="38"
                              style="display: none; overflow: scroll"></textarea>
                    <div id="result-breakpoint" class="form-control"
                         style="display: block; height: 98%; width: 100%; border: solid 1px; padding: 0;">
                        <div id="menu" class="" style="height: 100%; width: 100%; padding: 0; overflow: scroll">
                            {#                            <my-component v-bind:urls="urls"></my-component>#}
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>
    <p style="display: none" id="id-cache"></p>
    <template id="myComponent">
        <div class="diffModal modal fade" id="" tabindex="-1"
             role="dialog" aria-hidden="true">
            <div class="modal-dialog" style="width:1600px;">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close"
                                data-dismiss="modal"
                                aria-hidden="true">&times;
                        </button>
                        <p><a class="modal-title"
                              href=[[info.url]]
                              target="_blank">[[info.url]]</a></p>
                    </div>
                    <div id="" class="modal-body">
                        <!-- left column -->
                        <div class="col-md-6">
                            <div class="box box-primary">
                                <div class="box-body">
                                    <div class="form-group">
                                        <label class="control-label">
                                            原网页</label>
                                        <div class=" form-group has-success">
                                            <iframe class="iframe-detail"
                                                    src=""
                                                    width="735px"
                                                    height="600px"
                                                    scrolling="auto"
                                                    frameborder="0"></iframe>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- right column -->
                        <div class="col-md-6">
                            <div class="box box-warning">
                                <div class="box-body">
                                    <div>
                                        <span>标题、时间、内容 ...</span>
                                        <button class="btn-link pull-right-container collapse-content-button"
                                                tag-z="0"
                                                style="padding: 0px; border: 0; float: right"><span
                                                class="btn-text">隐藏</span>
                                            <span>
                                                    <i class="fa fa-angle-double-down pull-right"
                                                       style="font-size: large">
                                                    </i>
                                            </span>
                                        </button>
                                    </div>
                                    <p style="clear: both"></p>
                                    <div class="body-content" style="display: block">
                                        {#                                        <div class="dialog_allsite_title form-group has-success">#}
                                        {#                                            <label class="control-label">#}
                                        {#                                                标题&nbsp;&nbsp;#}
                                        {#                                            </label>#}
                                        {#                                            <p id=""#}
                                        {#                                               class=" allsite_title form-control"#}
                                        {#                                               style="overflow: scroll; height: 45px"#}
                                        {#                                               readonly="true">[[ info.title ]]</p>#}
                                        {#                                        </div>#}
                                        {#                                        <div id=""#}
                                        {#                                             class="dialog_allsite_ctime form-group has-success">#}
                                        {#                                            <label class="control-label">#}
                                        {#                                                时间&nbsp;&nbsp;#}
                                        {#                                            </label>#}
                                        {#                                            <p id=""#}
                                        {#                                               class="allsite_ctime form-control" style="overflow: scroll; height: 45px"#}
                                        {#                                               readonly="true"#}
                                        {#                                            >[[ info.ctime ]]</p>#}
                                        {#                                        </div>#}
                                        {#                                        <div id=""#}
                                        {#                                             class="dialog_allsite_source form-group has-success">#}
                                        {#                                            <label class="control-label">#}
                                        {#                                                来源&nbsp;&nbsp;#}
                                        {#                                            </label>#}
                                        {#                                            <p id=""#}
                                        {#                                               class="allsite_source form-control"#}
                                        {#                                               style="overflow: scroll; height: 45px"#}
                                        {#                                            >[[ info.source ]]</p>#}
                                        {#                                        </div>#}
                                        {##}
                                        {#                                        <div id=""#}
                                        {#                                             class="dialog_allsite_channel form-group has-success">#}
                                        {#                                            <label class="control-label">#}
                                        {#                                                频道&nbsp;&nbsp;#}
                                        {#                                            </label>#}
                                        {#                                            <p id=""#}
                                        {#                                               class="allsite_ctime form-control" style="overflow: scroll; height: 45px"#}
                                        {#                                               readonly="true"#}
                                        {#                                            >[[ info.channel ]]</p>#}
                                        {#                                        </div>#}
                                        <div v-for=" (key,val) in info">
                                            <div v-if="key != 'content_xml' && key != 'content' " id=""
                                                 class="dialog_allsite_channel form-group has-success">
                                                <label class="control-label">
                                                    [[ key ]]&nbsp;
                                                </label>
                                                <p id=""
                                                   class="allsite_ctime form-control"
                                                   style="overflow: scroll; height: 45px"
                                                   readonly="true"
                                                >[[ val ]]</p>
                                            </div>
                                        </div>

                                        <div id=""
                                             title=""
                                             class="dialog_allsite_content form-group has-success">
                                            <label>
                                                content&nbsp;&nbsp;
                                            </label>
                                            <div v-html="info.content" id="" class="allsite_content form-control"
                                                 style="min-height:300px; max-height: 600px; overflow:scroll;">

                                            </div>
                                        </div>
                                    </div>
                                    <div>
                                        <span style="color: #01a252">content_xml </span>
                                        <button class="btn-link pull-right-container collapse-xml-button"
                                                tag-z="1"
                                                style="padding: 0px; border: 0; float: right"><span
                                                class="btn-text">展开</span>
                                            <span>
                                                    <i class="fa fa-angle-double-down pull-right"
                                                       style="font-size: large"></i>
                                            </span>
                                        </button>
                                    </div>
                                    <div v-if="info.content_xml" id=""
                                         title="" style="display: none"
                                         class="dialog_allsite_content form-group has-success content-xml">
                                        <div v-html="info.content_xml" id="" class="allsite_content form-control"
                                             style="height: 600px; overflow:scroll;">

                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">

                    </div>
                </div>
            </div>
        </div>
    </template>
    <!-- 自定义字段的展示，需要使用过滤器-->

    <!-- message 弹出框-->
    <div class="modal fade" id="debug-message-window" tabindex="-1" role="dialog"
         aria-labelledby="myModalLabel" style="top:5%">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">×</span></button>
                    <h4 class="modal-title" id="debug-message-label">提示信息：</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group" style="max-height:500px">
                    </div>
                    <span id="debug-modal-content"></span>
                </div>
                <div class="modal-footer">
                    <button type="button" id="debug-message-close" class="btn btn-default"
                            data-dismiss="modal">关闭
                    </button>
                    <button type="button" id="debug-message-submit" class="btn btn-default"
                            data-dismiss="modal">确定
                    </button>
                </div>
            </div>
        </div>
    </div>
{% endblock section_content %}

{% block jquery_js %}

{% endblock jquery_js %}

{% block body_tail %}
    <script type="text/javascript" src="{% static 'zhongbao/js/csrf.js' %}"></script>
    <script src="{% static 'public/CodeMirror/lib/codemirror.js' %}"></script>
    <script src="{% static 'public/CodeMirror/mode/python/python.js' %}"></script>
    <script src="{% static 'zhongbao/config/js/vue_bak1.js' %}"></script>
    <script src="{% static 'public/html5shiv/js/jquery.slinky.js' %}"></script>
    <!-- page script -->
    <script type="text/javascript">
        var type2; // 分步测试，详情按钮点击后判断

        Vue.config.delimiters = ["[[", "]]"];

        $(document).ready(function () {
            var codeKey = "codekey";
            var storage = window.localStorage;
            var codeTag = "codetag";
            var code_text = storage.getItem(codeKey);
            var code_tag = storage.getItem(codeTag);
            var url_now = window.location.href;
            if (url_now.indexOf('add') != -1 && code_tag) {
                $('#codeView').val(code_text);
                storage.removeItem(codeTag);
            }

            function codem() {
                var textarea = document.getElementById('codeView');
                editor = CodeMirror.fromTextArea(textarea, {
                    mode: "python",
                    lineNumbers: true,
                    matchBrackets: true,
                    theme: "lesser-dark",
                    parserConfig: {useHTMLKludges: true}
                });
                editor.setOption("extraKeys", {
                    Tab: function (cm) {
                        var spaces = new Array(cm.getOption("indentUnit") + 1).join(" ");
                        cm.replaceSelection(spaces);
                    }
                });
                editor.setSize('750px', '700px');
                //textarea.innerHTML = editor.getValue();
                console.log("[info] codem() end.");
            }

            codem();


            $("#turn").click(function () {
                if (turn) {
                    $("#codeSection").attr("class", "col-md-12 connectedSortable");
                    $("#codeView").attr("rows", "14");
                    $("#resultSection").attr("class", "col-md-12 connectedSortable");
                    $("#result").attr("rows", "16");
                    editor.setSize('1550px', '300px');
                } else {
                    $("#codeSection").attr("class", "col-md-6 connectedSortable");
                    $("#codeView").attr("rows", "35");
                    $("#resultSection").attr("class", "col-md-6 connectedSortable");
                    $("#result").attr("rows", "38");
                    editor.setSize('750px', '700px');
                }
                turn = !turn;
            });

            $("#codeColor").click(function () {
                codem();
            });


        });

        // 配置标志获取
        var config_flag = $('#config-flag').text();
        // web可视化爬虫配置入口url
        var entrance_url = $('#entrance_url').text();
        var web_config_flag;    // 判断分级检测在第几层
        var detail_judge = 0;   // 判断是否是详情页
        // http://172.16.254.15:8000/webSpider/hubs/?start_url=http://www.people.com.cn/&level=1  非详情页跳转基本链接
        var base_web_spider_url = ""
        // http://172.16.254.15:8000/webSpider/detail/?start_url=http://www.people.com.cn/  详情页跳转基本链接
        var base_web_spider_detail = ""

        var type123 = ''
        var cnt = 0;
        var uuid;
        var proxy_flag;
        var code = "";
        var clock;
        var turn = true;
        var editor;
        var debug_tag;
        // var url_inland = 'http://192.168.148.99:8022/';
        var url_inland = '';
        // var url_inland = 'http://192.168.132.79/';
        var url_outland = '';
        var url;
        var func_name;
        var func_name_org = null;
        var url_debug;
        var api_debug_url = $('#api_debug_url').text();
        var api_debug_result_url = $('#api_debug_result_url').text();
        var api_debug_kill_url = $('#api_debug_kill_url').text();
        var break_res = $('#result-breakpoint');
        var res_id;
        var res_dom;
        var back_title;

        var child1 = Vue.extend({
            template: '#myComponent',
            methods: {
                greet: function (event) {
                    // `this` 在方法里指当前 Vue 实例
                    console.log($(this).parent().text());
                    // `event` 是原生 DOM 事件
                    console.log(event.target.tagName)
                }
            },
            props: ['info',]

        });

        function checkService(type1, func) {
            $.ajax({
                url: api_debug_url,
                type: 'POST',
                data: {
                    csrfmiddlewaretoken: '{{ csrf_token }}',
                    tag: 'check_service',
                    bb: '{{ bb }}',
                    type: type1,

                },
                error: function (xhr, err) {
                    $("#status").text("超时3秒，后台服务异常！").attr("class", "label label-danger");
                    window.clearInterval(clock);
                    console.log("[error] /webspider/alive:" + err);
                },
                success: function (data, status) {
                    if (data == 1) {
                        console.log("[info] checkService() end.", data);
                        func(type1);
                    } else {
                        $("#status").text("超时3秒，爬虫服务异常！").attr("class", "label label-danger");
                        window.clearInterval(clock);
                    }

                    //$("#status").text("服务正常");
                }
            });
        }

        function getStatus() {
            var url_post = url + 'webspider/status';
            $.ajax({
                url: api_debug_url,
                type: 'POST',
                data: {
                    tag: 'getstatus',
                    code: uuid,
                    bb: '{{ bb }}',
                    type: type123,
                },
                error: function (xhr, err) {
                    console.log("[error] /webspider/status err:", xhr, err);
                },
                success: function (data, status) {
                    console.log(data, "123123123");
                    var msg = "";
                    debug_tag = data;
                    if (data == 1) {
                        msg = "任务完成";
                        window.clearInterval(clock);
                    } else if (data == 0) {
                        msg = "尚未完成";
                    } else {
                        msg = "任务超时，被测试机自动清理！";
                        window.clearInterval(clock);
                    }
                    $("#status").text(msg);
                    console.log("[info] getStatus() end.123123", data);
                }
            });
        }

        function web_config_error(par_ele, text_n, web_config_tag) {
            var href;
            var res_web_config_tag = web_config_tag - 1;
            if (detail_judge == 0) {
                href = base_web_spider_url + res_web_config_tag + '&start_url=' + entrance_url;
            } else {
                href = base_web_spider_detail + entrance_url;
            }
            // 请返回以下界面进行修改
            // par_ele.text(text_n);
            var $li_span = $('<span></span>');
            var $li_p = $('<p></p>');
            var $li_a1 = $('<a target="_blank" style="color:red"></a>');
            $li_span.text('请返回以下url进行修改: ');
            $li_a1.attr('href', href);
            $li_a1.text(href);
            par_ele.append($li_span);
            $li_p.append($li_a1);
            par_ele.append($li_p);
        }

        function getResult(type) {
            if (uuid == "") {
                return false;
            }
            // $("#result").val("");
            var url_post = url + 'webspider/code_result?code=' + uuid;
            $.ajax({
                url: api_debug_result_url,
                type: 'POST',
                data: {
                    tag: 'getresult',
                    url: url_post,
                    uuid: uuid,
                    type: type,
                    bb: '{{ bb }}',
                },
                error: function (xhr, err) {
                    if (type == 2 && type2) {
                        type2.find('.menu-num').each(function () {
                            $(this).removeAttr('disabled');
                        });
                    }
                    alert('获取结果时服务器发生错误，请稍后再试！');
                    console.log("[error] /webspider/code_result:", err);
                },
                success: function (data, status) {
                    data = eval(data);
                    if (type == 2 && type2) {
                        type2.find('.menu-num').each(function () {
                            $(this).removeAttr('disabled');
                        });
                    }
                    if (type == 2) {
                        var base_id = res_dom.attr('id');
                        console.log(base_id);
                    }

                    console.log(uuid);
                    console.log(data['code']);
                    if (data['code'] == uuid) {
                        var real_result = data['result'];
                        console.log('real_result');
                        console.log(real_result);
                        if (data['result'] == 'uuid') {
                            $("#status").text(data['status']);
                        } else {
                            if (type == 2) {
                                console.log(data);
                                var result_content = JSON.parse(real_result);
                                if (result_content['code'] != 0) {
                                    var $div = $('<div class="text-error" style="margin-bottom: 10px;"></div>');
                                    var $div_p = $('<p></p>');
                                    if (config_flag == '3') {
                                        var web_text_error1 = '发生错误，错误信息如下：';
                                        console.log('result_content');
                                        console.log(result_content['error']);
                                        $div.text(web_text_error1);
                                        $div_p.text(result_content['error']);
                                        $div.append($div_p);
                                        web_config_error($div, web_text_error1, web_config_flag)
                                    } else {
                                        $div.text(result_content['error']);
                                        $div.children('p').text('请查找原因后，重新测试！');
                                    }
                                    res_dom.append($div);

                                } else {
                                    if (data['content'] == 0) {
                                        var urls = result_content['urls'];
                                        var $ul = $('<ul></ul>');
                                        if (urls.length == 0) {
                                            var $li_none = $('<li class="text-error"></li>');
                                            if (config_flag == '3') {
                                                var web_text_error2 = '发生错误，没有返回结果。';
                                                $li_none.text(web_text_error2);
                                                web_config_error($li_none, web_text_error2, web_config_flag)
                                            } else {
                                                $li_none.text('没有返回结果，请查找是否有时间过滤、间隔过滤等影响！');
                                            }
                                            $ul.append($li_none);
                                        } else {
                                            {#                                        res_dom_num.text(res_dom_num.text() + '-' + urls.length);#}
                                            for (var i in urls) {
                                                var $li = $('<li class="back-li"><p class=""><a href="" target="_blank" class="menu-a"></a><button class="menu-num btn-link">详情</button></p></li>');
                                                var $li_p = $li.find('p');
                                                var $li_a = $li.find('a');
                                                var $li_button = $li.find('.menu-num');
                                                $li.attr('id', base_id + i);
                                                var $li_span = $li.find('span');
                                                console.log(urls[i]);
                                                var url_r = urls[i];
                                                url_r['url-title'] = url_r['url'];
                                                if (url_r['url'] == null) {
                                                    url_r['url'] = '函数无返回url，点击详情继续';
                                                    url_r['url-title'] = '';
                                                }
                                                ;
                                                if (url_r['is_next'] == '1') {
                                                    $li_button.text('下一页');
                                                }
                                                $li_p.attr('next-func', url_r['next']);
                                                $li_a.text(url_r['url']);
                                                $li_a.attr('title-href', url_r['url-title']);
                                                $li_a.attr('href', url_r['url']);
                                                $li_a.attr('post_data', url_r['post_data']);
                                                $ul.append($li);
                                            }
                                        }
                                        res_dom.append($ul);
                                        if (res_dom[0].tagName == 'LI') {
                                            console.log('li');
                                            var res_dom_num = res_dom.children('p').find('.menu-num');
                                            res_dom_num.text(res_dom_num.text() + '(' + urls.length + ')');
                                            var $li_back = $('<li><p class="menu-back"><a href="#" class="menu-back-a">返回上一级</a><span class="menu-title"></span><span class="menu-urls-num"></span></p></li>');
                                            $li_back.find('.menu-title').text(back_title);
                                            $li_back.find('.menu-urls-num').text('num:' + urls.length);
                                            $ul.prepend($li_back);

                                            res_dom.parent().children('li').each(function () {
                                                if ($(this).html() != res_dom.html()) {
                                                    $(this).css('display', 'none');
                                                }
                                            });
                                            res_dom.children('p').css('display', 'none');

                                            {#                                    res_dom.attr('display', 'block');#}
                                        } else {
                                            var $li_back2 = $('<li><p class="menu-back"><a href="#" class="menu-back-a"></a><span class="menu-title">第一层获取</span><span class="menu-urls-num"></span></p></li>');
                                            $li_back2.find('.menu-urls-num').text('num:' + urls.length);
                                            $ul.prepend($li_back2);
                                        }

                                    } else {
                                        var my_component = $('<my-component v-bind:info="info"></my-component>');
                                        res_dom.append(my_component);
                                        var real_data = JSON.parse(data['real_content']);
                                        console.log(real_data);
                                        console.log(real_data.length);
                                        var content_fields = ['title', 'ctime', 'source', 'channel', 'content',];
                                        var content_vm = new Vue({
                                            el: '#' + base_id,
                                            data: {
                                                info: real_data,
                                            },
                                            components: {
                                                'my-component': child1
                                            }
                                        });
                                        var detail_modal = res_dom.children('.diffModal');
                                        var iframe = detail_modal.find('.iframe-detail');
                                        iframe.attr('src', res_dom.find('.menu-a').text());
                                        detail_modal.modal();
                                    }
                                }
                            } else {
                                $("#result").val(real_result);
                            }
                        }
                    }
                    {#                        if(debug_tag==1){#}
                    {#                            $("#result").val('代码正常');#}
                    {#                        }#}
                    console.log("[info] getResult() end. uuid:", uuid);
                }
            });
        }

        function refresh(type) {
            if (cnt > 200) {
                window.clearInterval(clock);
                $("#kill").click();
                var msg = '请求超时，终止任务！请稍后再试或联系管理员！';
                if (type == 2) {
                    $('#result-breakpoint').text(msg);
                } else {
                    $('#result').text(msg);
                }
            } else {
                cnt++;
                console.log("[info] refresh() cnt:", cnt);
                getStatus();
                getResult(type);
            }
        }

        function get_service(type) {
            {#                console.log("[info] start() proxy:", proxy_flag, "type:", type, "code:", code);#}
            console.log("[info] start() proxy:", proxy_flag, "type:", type);

            $("#status").text("测试任务开始").attr('class', 'badge');
            clock = window.setInterval(function () {
                refresh(type)
            }, 3000); // 1s
            var url_post = url + 'webspider/run_code';
            $.ajax({
                url: api_debug_url,
                type: 'POST',
                data: {
                    tag: 'runcode',
                    type: type,
                    proxy: proxy_flag,
                    code: code,
                    func_name: func_name,
                    url_debug: url_debug,
                    bb: '{{ bb }}',
                },
                error: function (xhr, err) {
                    console.log("[error] /webspider/run_code:", err);
                },
                success: function (data, status) {
                    console.log("[info] start() end. uuid:", data);
                    if (data == '') {
                        window.clearInterval(clock);
                        return false;
                    }
                    else if (data === "测试机禁止链接redis和mysql") {
                        msg = "测试机禁止链接redis和mysql!";
                         window.clearInterval(clock);
                         $("#status").text("任务终止");
                         alert(msg);
                         return false;
                    }
                    uuid = data;
                }
            });

        }

        function start(type) {
            cnt = 0;
            $("#result").val("");
            $("#status").text("连接测试服务器");
            proxy_flag = $("input[class='debug-radio']:checked").val();
            var env_flag = $("input[class='env-radio']:checked").val();
            if (env_flag == '0') {
                url = url_inland;
            } else {
                url = url_outland;
            }
            //var code = $("#codeView").val();
            code = editor.getValue();
            if (code == "") {
                alert("代码不能为空");
                return false;
            }
            checkService(type, get_service);
        }


        $("#single").click(function () {
            window.clearInterval(clock);
            $("#result").css('display', 'block');
            break_res.css('display', 'none');
            type123 = '0'
            start(0);
        });

        $("#continue").click(function () {
            window.clearInterval(clock);
            $("#result").css('display', 'block');
            break_res.css('display', 'none');
            type123 = '1'
            start(1);
        });

        $('#breakpoint').click(function () {
            window.clearInterval(clock);
            $("#result").css('display', 'none');
            break_res.css('display', 'block');
            $("#menu").html("");
            url_debug = null;
            func_name = null;
            type123 = '2'
            res_id = 'menu';
            res_dom = $('#menu');
            web_config_flag = 0;
            detail_judge = 0;

            start(2);
        });

        // '.body-content'
        function collapse_con(this_ele, body_content_class) {
            var body_content = this_ele.parent().parent().find(body_content_class);
            var tag = this_ele.find('i');
            var btn_text = this_ele.find('.btn-text');
            if (this_ele.attr('tag-z') == '0') {
                this_ele.attr('tag-z', '1');
                btn_text.text('展开');
                tag.removeClass('fa-angle-double-down');
                tag.addClass('fa-angle-double-left');
                body_content.css('display', 'none');
            } else {
                this_ele.attr('tag-z', '0');
                btn_text.text('隐藏');
                tag.removeClass('fa-angle-double-left');
                tag.addClass('fa-angle-double-down');
                body_content.css('display', 'block');
            }
        }

        $("#menu").on('click', '.collapse-content-button', function () {
            collapse_con($(this), '.body-content');
        });
        $("#menu").on('click', '.collapse-xml-button', function () {
            collapse_con($(this), '.content-xml');
        });
        {#        $('.collapse-content-button').click(function () {#}
        {#            var tag = $(this).find('i');#}
        {#            alert($(this).attr('tag-z'));#}
        {#            if ($(this).attr('tag-z') == '0') {#}
        {#                $(this).attr('tag-z', '1');#}
        {#                tag.removeClass('fa-angle-double-left');#}
        {#                tag.addClass('fa-angle-double-down');#}
        {#            } else {#}
        {#                $(this).attr('tag-z', '0');#}
        {#                tag.removeClass('fa-angle-double-down');#}
        {#                tag.addClass('fa-angle-double-left');#}
        {#            }#}
        {#        });#}

        $('#menu').on('click', '.menu-num', function () {
            var par = $(this).parent();
            res_dom = par.parent();
            if (res_dom.children('.diffModal').length > 0) {
                console.log('diffModal');
                var detail_modal = res_dom.children('.diffModal');
                var iframe = detail_modal.find('.iframe-detail');
                // iframe.attr('src', par.find('.menu-a').text());
                detail_modal.modal();
            } else if (res_dom.children('ul').length > 0) {
                res_dom.parent().children('li').each(function () {
                    if ($(this).html() != res_dom.html()) {
                        $(this).css('display', 'none');
                    }
                });
                res_dom.children('p').css('display', 'none');
                res_dom.children('ul').css('display', 'block');
            } else {
                $(this).attr('disabled', 'disabled');
                type2 = res_dom.parent();
                type2.find('.menu-num').each(function () {
                    $(this).attr('disabled', 'disabled');
                });
                console.log(res_dom[0].tagName);
                url_debug = par.children('a').attr('post_data');
                back_title = $(this).prev().attr('title-href');
                func_name = par.attr('next-func');
                if (config_flag == '3') {
                    if (func_name == 'parse') {
                        detail_judge = 0;
                        web_config_flag = 1;
                    } else if (func_name == 'parse_detail_page') {
                        web_config_flag += 1;
                        detail_judge = 1
                    } else {
                        detail_judge = 0;
                        var func_tag = func_name.replace('parse_next_', '');
                        web_config_flag = parseInt(func_tag);
                    }
                }
                start(2);
            }

        });

        $('#menu').on('click', '.menu-back-a', function () {
            var par = $(this).closest('ul').parent();
            var grand_par = par.parent();
            par.children('ul').css('display', 'none');
            par.children('p').css('display', 'block');
            grand_par.children('li').each(function () {
                $(this).css('display', 'block');
            });
        });

        $("#kill").click(function () {
            var url_post = url + 'webspider/kill';
            console.log("[info] kill() start.");
            $.ajax({
                url: api_debug_kill_url,
                type: 'POST',
                data: {
                    code: uuid,
                    url: url_post,
                    bb: '{{ bb }}',
                    type: type123,
                },
                error: function (xhr, err) {
                    $("#status").text("任务终止失败！");
                    console.log("[error] /webspider/kill" + err);
                },
                success: function (data, status) {
                    window.clearInterval(clock);
                    // $("#result").val("");
                    window.setTimeout(function () {
                        $("#status").text("任务终止");
                    }, 3000);

                    console.log("[info] kill() end.", data);
                }
            });
        });

        $('#save-config').click(function () {
            var mes = '本页面进行的代码修改不会保存，如果要修改配置，请点击返回修改按钮！';
            $('#debug-modal-content').text(mes);
            $('#debug-modal-content').addClass('base-alert-warning');
            $('#debug-message-window').modal();
        });
        $('#debug-message-submit').click(function () {
            $('#real-save').click();
        });


    </script>
{% endblock body_tail %}
